//
//  CellOfShop.m
//  PetAPP
//
//  Created by apple on 16/2/15.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "CellOfShop.h"
#import "ModelOfShop.h"
#import <YYWebImage.h>

@interface CellOfShop ()

@property (nonatomic, retain) UIView *viewOfCell;

@property (nonatomic, retain) UIImageView *imageViewOfPic;

@property (nonatomic, retain) UILabel *labelOftitle;

@property (nonatomic, retain) UILabel *labelOftime;

@property (nonatomic, retain) UILabel *labelOfTime2;


@end

@implementation CellOfShop

- (void)dealloc {

    [_viewOfCell release];
    [_imageViewOfPic release];
    [_labelOftitle release];
    [_labelOftime release];
    [_labelOfTime2 release];
    [super dealloc];

}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

- (void)createSubView {
    
    self.viewOfCell = [[UIView alloc] init];
    [self.contentView addSubview:_viewOfCell];
    [_viewOfCell release];
    self.viewOfCell.backgroundColor = [UIColor whiteColor];
    
    
    self.imageViewOfPic = [[UIImageView alloc] init];
    [self.viewOfCell addSubview:_imageViewOfPic];
    [_imageViewOfPic release];

    
    self.labelOftitle = [[UILabel alloc] init];
    [self.viewOfCell addSubview:_labelOftitle];
    [_labelOftitle release];
    self.labelOftitle.font = [UIFont systemFontOfSize:18];
    
    
    self.labelOftime = [[UILabel alloc] init];
    [self.viewOfCell addSubview:_labelOftime];
    [_labelOftime release];
    
    self.labelOftime.font = [UIFont systemFontOfSize:15];
    self.labelOftime.textColor = [UIColor orangeColor];
    
    self.labelOftime.textAlignment = NSTextAlignmentRight;
    
    
    self.labelOfTime2 = [[UILabel alloc] init];
    [self.viewOfCell addSubview:_labelOfTime2];
    [_labelOfTime2 release];
    self.labelOfTime2.font = [UIFont systemFontOfSize:15];
    self.labelOfTime2.textColor = [UIColor grayColor];
    self.labelOfTime2.textAlignment = NSTextAlignmentRight;
    self.labelOfTime2.text = @"距结束";
}

- (void)layoutSubviews {

    [super layoutSubviews];
    
    self.viewOfCell.frame = CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width - 20, 140);
    if (self.flag) {
       self.viewOfCell.frame = CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width - 20, 140);
    }
    
    
    self.imageViewOfPic.frame = CGRectMake(5, 5, self.viewOfCell.frame.size.width - 10, 100);
    
    self.labelOftitle.frame = CGRectMake(5, 110, 200, 20);
    
    CGRect labelOfFrame = [self.labelOftime.text boundingRectWithSize:CGSizeMake(0, 17) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    
    self.labelOftime.frame = CGRectMake(self.viewOfCell.frame.size.width - 5 - labelOfFrame.size.width - 10 , 112, labelOfFrame.size.width, 17);

    self.labelOfTime2.frame = CGRectMake(self.viewOfCell.frame.size.width - self.labelOftime.frame.size.width - 17 - 70, 112, 70, 17);
    

}



- (void)handleDataWithModel:(ModelOfShop *)model {
    
    self.imageViewOfPic.yy_imageURL = [NSURL URLWithString:model.topic_img];
    [self.imageViewOfPic yy_setImageWithURL:[NSURL URLWithString:model.topic_img] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];

    
    self.labelOftitle.text = model.title;
    
    
    
    /** 用结束时间减去当前时间，剩下多少天 */
    NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:model.end_time.intValue];
    NSDate *now = [NSDate date];
    NSTimeInterval secondsInterval= [endDate timeIntervalSinceDate:now];
    float points = secondsInterval / 60 / 60 / 24;
    /** 通过函数向上取整 */
    self.labelOftime.text = [NSString stringWithFormat:@"%d天", (int)points];

}

@end
